package cn.doourbest.learnspring.security;

import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisShardInfo;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @description:
 * @author: zhangsc
 * @create: 2019/11/4
 */

@Configuration
@Component
public class CustomerAuthorizationServerConfigurer implements AuthorizationServerConfigurer {


    @PostConstruct
    private void init(){
        System.out.println(1);
    }

    @Bean
    @Primary
    public RedisTokenStore tokenStore(){


        JedisShardInfo jedisShardInfo = new JedisShardInfo("127.0.0.1","6379");
        jedisShardInfo.setPassword("root");


        RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(jedisShardInfo);

        RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);

        return redisTokenStore;
    }


    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security.addTokenEndpointAuthenticationFilter(customerBasicAuthenticationFilter());

        security.allowFormAuthenticationForClients();


    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore());
    }


    @Bean
    public CustomerBasicAuthenticationFilter customerBasicAuthenticationFilter(){
        return new CustomerBasicAuthenticationFilter();
    }
}
